/*

 This is a simple card game simulator, where you try to beat the computer by
 obtaining a stronger card. Sort of an "indian poker" kind of game, I guess.

 Copyright 2022 - kzimmermann - https://tilde.town/~kzimmermann
 This program is Free Software licensed and released under the GNU GPL v3.
 See <https://www.gnu.org/licenses> for more information

 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h> // for pseudorandom number
#include <unistd.h> // for sleep()
#include <string.h>
#include "cards.h"

#define BUFF 25 // buffer size for reading a line
#define MAX_SCORE 500 // how much is the winning threshold of the game?
#define MIN_BET 10 // minimum bet!

int 
main(int argc, char* argv[]) {
    // seed the RNG:
    srand(time(NULL));
    
    // player's "hand"
    struct playingcard mine;
    
    // opponent's hand:
    struct playingcard theirs;

    // Initial purse money:
    int purse = 100;

    // bet container
    int bet = 0;

    // are ya winning, son?
    int win = 0;
    
    // limits for betting:
    int bet_max, bet_min = 0;

    // placeholder string
    char line[BUFF];
    
    while(1) {
        mine = draw_card();
        theirs = draw_card();

        // clear screen:
        printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
        printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
        printf("\n------------------------------------\n");

        printf("Your current purse: %d\n", purse);

        if (purse > MIN_BET) {
            bet_min = MIN_BET;
            bet_max = purse;
        }
        else {
            // uh-oh you have less than the minimum amount!
            bet_min = purse;
            bet_max = purse;
        }
        
        printf("Your hand: ");
        int status = describe_card(mine);
        if (status != 0) {
            printf("Error: malformed card.\n");
            return 1;
        }

        printf("Proceed? (1=proceed, 0=fold) ");
        int decision = 0;
        fgets(line, BUFF - 1, stdin);
        sscanf(line, "%d", &decision);

        if (decision == 1) {
            while(1) {
                printf("Your bet? (%d ~ %d) ", bet_min, purse);
                fgets(line, BUFF - 1, stdin);
                sscanf(line, "%d", &bet);
                if (bet < bet_min || bet > bet_max) {
                    printf("Choose a bet between your limits!\n");
                }
                else
                    break;
            }
        }
        
        printf("Opponent's hand: ");
        status = describe_card(theirs);
        if (status != 0) {
            printf("Error: malformed card.\n");
            return 1;
        }

        if (decision == 0) {
            printf("However, you've folded, so you lose %d!\n", MIN_BET);
            purse -= MIN_BET; // no more cheating!
        }
        
        else {
        
            if (mine.value > theirs.value) {
                printf("You win!\n");
                win = 1;
            } else if (mine.value == theirs.value && mine.suit > theirs.suit) {
                printf("You win!\n");
                win = 1;
            } else {
                printf("You lose the round.\n");
                win = 0;
            }
            if (win == 1) {
                printf("You gain %d\n", bet);
                purse += bet;
            }
            else {
                printf("You lose %d\n", bet);
                purse -= bet;
            }
            if (purse <= 0) {
                printf("\n***Game over***\n");
                break;
            } else if (purse >= MAX_SCORE) {
                printf("\n*** You've made over %d in the game. Congrats, you win!\n", MAX_SCORE);
                break;
            }
        }

        // add a little suspense >:)
        sleep(1);

    }
    return 0;
}
